package com.jcfk.eam.controller.main;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.*;
import com.jcfk.eam.domain.dto.main.EamPointCheckDetailDTO;
import com.jcfk.eam.domain.dto.main.EamPointCheckDetailImgDTO;
import com.jcfk.eam.domain.dto.main.EamPointCheckOrderDTO;
import com.jcfk.eam.domain.po.main.EamPointCheckOrder;
import com.jcfk.eam.domain.query.main.EamPointCheckOrderQuery;
import com.jcfk.eam.service.main.EamPointCheckDetailService;
import com.jcfk.eam.service.main.EamPointCheckOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 资产点检工单
 *
 * @author yrl
 * @email 
 * @date 2024-11-06
 */
@Api(tags = {"资产点检工单管理"})
@RestController
@RequestMapping("eampointcheckorder")
public class EamPointCheckOrderController {
    @Autowired
    private EamPointCheckOrderService eamPointCheckOrderService;
    @Autowired
    private EamPointCheckDetailService eamPointCheckDetailService;

    /**
     * 分页列表
     */
    @ApiOperation("分页查询资产点检工单")
    @PostMapping("/queryByPage")
    public RetPage<EamPointCheckOrderDTO> queryByPage(@RequestBody ReqPageParam<EamPointCheckOrderQuery> requestParam) {
        //设置默认排序
        if(requestParam.getSort().getOrderItem()==null){
            ReqSortParam<EamPointCheckOrderQuery> sortParam=new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }
        DataPage<EamPointCheckOrderDTO> page = new DataPage<EamPointCheckOrderDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            EamPointCheckOrderDTO.class);
        page = eamPointCheckOrderService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamPointCheckOrderQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取资产点检工单列表", notes = "获取资产点检工单列表", httpMethod = "POST")
    @PostMapping("getDetails")
    public RetResult<List<EamPointCheckDetailDTO>> getDetails(@RequestBody EamPointCheckOrderQuery query) {

        List<EamPointCheckDetailDTO> list = eamPointCheckOrderService.getDetails(query);

        return RetResult.ok(list);
    }

    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamPointCheckOrderQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取资产点检工单列表", notes = "获取资产点检工单列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<List<EamPointCheckOrderDTO>> getList(@RequestBody EamPointCheckOrderQuery query) {

        List<EamPointCheckOrderDTO> list = eamPointCheckOrderService.getList(query);

        return RetResult.ok(list);
    }


    /**
     *  资产点检工单
     * 根据id 查询详情
     */
    @ApiOperation("查询资产点检工单")
    @GetMapping("/get/{id}")
    public RetResult<EamPointCheckOrderDTO> get(@PathVariable("id") String id) {
            EamPointCheckOrderDTO item = eamPointCheckOrderService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理资产点检工单
     */
    @ApiOperation("保存资产点检工单")
    @PostMapping("/save")
    @RequestLogHeader(modular = "资产点检工单管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamPointCheckOrderDTO eamPointCheckOrder) {
        // 数据校验
        RetResult ret = eamPointCheckOrderService.isValid(eamPointCheckOrder);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(eamPointCheckOrder.getId())){
            // 新增
                eamPointCheckOrderService.insert(eamPointCheckOrder);
        } else{
            // 更新
                eamPointCheckOrderService.update(eamPointCheckOrder);
        }
        return RetResult.ok();

    }


    /**
     *  批量删除资产点检工单
     */
    @ApiOperation("批量删除资产点检工单")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "资产点检工单管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamPointCheckOrderDTO eamPointCheckOrder) {
        return RetResult.ok(eamPointCheckOrderService.logicDeleteList(eamPointCheckOrder));
    }


    /**
     *  删除资产点检工单
     */
    @ApiOperation("删除资产点检工单")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "资产点检工单管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamPointCheckOrderService.deleteItem(id);
        return RetResult.ok();
    }


    @ApiOperation("指定模具code查询点检详情")
    @GetMapping("/getDetailByAssetCode")
    public RetResult<?> getDetailByAssetCode(String assetCode) {
        Assert.notEmpty(assetCode, "模具编码不能为空");
        return RetResult.ok(eamPointCheckOrderService.getDetailByAssetCode(assetCode));
    }
}
